//------------------------------------------------------------------------------
//	Original license:
//	Copyright 2001, 2002 Georges Menie (www.menie.org)
//	stdarg version contributed by Christian Ettinger
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU Lesser General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//------------------------------------------------------------------------------
#include <stdint.h>
#include <stdbool.h>

#include <stdarg.h>
#include <stdio.h>

#include "pfc.h"

//------------------------------------------------------------------------------
//	Pad enum
//------------------------------------------------------------------------------
enum {
    PAD_RIGHT	= 1,
    PAD_ZERO	= 2,
};

//------------------------------------------------------------------------------
//!   printchar: print a character
//!
//! @param
//!		char		**str:		output string; if void, direct output to port
//!		int			c:			value to output
//!
//! @return
//!    void
//!
//!	Notes:
//!	* This is a local (static) function.
//! * The purchar function is defined by the Keil library.
//!   It's not clear where this function will send the characters
//------------------------------------------------------------------------------
static void pc(int c)
{
	putchar(c);
}
static void printchar(char **str, int c)
{
	if (str) {
		**str = c;
		++(*str);
	} else pc(c);
}

//------------------------------------------------------------------------------
//!   prints: print a string
//!
//! @param
//!		char 		**out:		output buffer
//!		const char	*string:	output string
//!		int			width:		output buffer limit
//!		int			pad:		padding information: left/right, zero/space
//!
//! @return
//!    Number of bytes output
//!
//!	Note: This is a local (static) function.
//------------------------------------------------------------------------------
static int prints(char **out, const char *string, int width, int pad)
{
	register int pc = 0, padchar = ' ';

	if (width > 0) {
		register int len = 0;
		register const char *ptr;
		for (ptr = string; *ptr; ++ptr) ++len;
		if (len >= width) width = 0;
		else width -= len;
		if (pad & PAD_ZERO) padchar = '0';
	}
	if (!(pad & PAD_RIGHT)) {
		for (; width > 0; --width) {
			printchar(out, padchar);
			++pc;
		}
	}
	for (; *string ; ++string) {
		printchar(out, *string);
		++pc;
	}
	for (; width > 0; --width) {
		printchar(out, padchar);
		++pc;
	}

	return pc;
}


//------------------------------------------------------------------------------
//!   printi: print an integer
//!
//! @param
//!		char 		**out:		output buffer
//!		int			i:			output value
//!		int			b:			output radix (10 or 16 right now)
//!		int			sg:			output sign (positive / negative)
//!		int			width:		output buffer limit
//!		int			pad:		padding information: left/right, zero/space
//!		int			letbase:	letter base (a..f or A..F in hex)
//!
//! @return
//!    Number of bytes output
//!
//!	Note: This is a local (static) function.
//------------------------------------------------------------------------------
#define PRINT_BUF_LEN		12
static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
{
	char print_buf[PRINT_BUF_LEN];
	register char *s;
	register int t, neg = 0, pc = 0;
	register unsigned int u = i;

	if (i == 0) {
		print_buf[0] = '0';
		print_buf[1] = '\0';
		return prints(out, print_buf, width, pad);
	}

	if (sg && b == 10 && i < 0) {
		neg = 1;
		u = -i;
	}

	s = print_buf + PRINT_BUF_LEN-1;
	*s = '\0';

	while (u) {
		t = u % b;
		if (t >= 10)
			t += letbase - '0' - 10;
		*--s = t + '0';
		u /= b;
	}

	if (neg) {
		if (width && (pad & PAD_ZERO)) {
			printchar(out, '-');
			++pc;
			--width;
		} else {
			*--s = '-';
		}
	}

	return pc + prints(out, s, width, pad);
}

//------------------------------------------------------------------------------
//!	print: printf like print formatter -- standard version
//!	supports:
//!	%s:	string
//!	%c:	character
//!	%b:	binary
//!	%d:	decimal (signed)
//!	%u:	decimal (unsigned)
//!	%x:	hex (lower case)
//!	%X:	hex (upper case)
//!
//! @param
//!		char 		**out:		output buffer
//!		int			*vargs:		input argument pointer
//!
//! @return
//!    Number of bytes output
//!
//!	Note: This is a local (static) function.
//------------------------------------------------------------------------------
static int print(char **out, int *varg)
{
	register int width, pad;
	register int pc = 0;
	register char *format = (char *)(*varg++);
	char scr[2];
	register char *s = '\0';

	for (; *format != 0; ++format) {
		if (*format == '%') {
			++format;
			width = pad = 0;
			if (*format == '\0') break;
			if (*format == '%') goto out;
			if (*format == '-') {
				++format;
				pad |= PAD_RIGHT;
			}
			while (*format == '0') {
				++format;
				pad |= PAD_ZERO;
			}
			for (; *format >= '0' && *format <= '9'; ++format) {
				width *= 10;
				width += *format - '0';
			}
			switch (*format) {
			case 's':
				s = *((char **)varg++);
				pc += prints(out, s?s:"(null)", width, pad);
				break;
			case 'c':
				scr[0] = *varg++;
				scr[1] = '\0';
				pc += prints(out, scr, width, pad);
				break;

			case 'b':
				pc += printi(out, *varg++, 2, 0, width, pad, 'a');
				break;
			case 'd':
				pc += printi(out, *varg++, 10, 1, width, pad, 'a');
				break;
			case 'u':
				pc += printi(out, *varg++, 10, 0, width, pad, 'a');
				break;

			case 'x':
				pc += printi(out, *varg++, 16, 0, width, pad, 'a');
				break;
			case 'X':
				pc += printi(out, *varg++, 16, 0, width, pad, 'A');
				break;
			}
		} else {
out:
			printchar(out, *format);
			++pc;
		}
	}
	if (out) **out = '\0';
	return pc;
}

//------------------------------------------------------------------------------
//!	print_va: printf like print formatter -- va_list version
//!	supports:
//!	%s:	string
//!	%c:	character
//!	%b:	binary
//!	%d:	decimal (signed)
//!	%u:	decimal (unsigned)
//!	%x:	hex (lower case)
//!	%X:	hex (upper case)
//!
//! @param
//!		char 		**out:		output buffer
//!		const char	*format:	printf format pointer
//!		va_list		args:		input argument pointer
//!
//! @return
//!    Number of bytes output
//!
//!	Note: This is a local (static) function.
//------------------------------------------------------------------------------
static int print_va(char **out, const char *format, va_list args)
{
	register int width, pad;
	register int pc = 0;
	char scr[2];
	register char *s;

	for (; *format != 0; ++format) {
		if (*format == '%') {
			++format;
			width = pad = 0;
			if (*format == '\0') break;
			if (*format == '%') goto out;
			if (*format == '-') {
				++format;
				pad |= PAD_RIGHT;
			}
			while (*format == '0') {
				++format;
				pad |= PAD_ZERO;
			}
			for (; *format >= '0' && *format <= '9'; ++format) {
				width *= 10;
				width += *format - '0';
			}
			switch (*format) {
			case 's':
				s = (char *)va_arg(args, int);
				pc += prints(out, s?s:"(null)", width, pad);
				break;
			case 'c':
				scr[0] = (char)va_arg(args, int);
				scr[1] = '\0';
				pc += prints(out, scr, width, pad);
				break;

			case 'b':
				pc += printi(out, va_arg(args, int), 2, 0, width, pad, 'a');
				break;
			case 'd':
				pc += printi(out, va_arg(args, int), 10, 1, width, pad, 'a');
				break;
			case 'u':
				pc += printi(out, va_arg(args, int), 10, 0, width, pad, 'a');
				break;

			case 'x':
				pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'a');
				break;
			case 'X':
				pc += printi(out, va_arg(args, int), 16, 0, width, pad, 'A');
				break;
			}
		} else {
out:
			printchar(out, *format);
			++pc;
		}
	}
	if (out) **out = '\0';
	va_end(args);
	return pc;
}

//------------------------------------------------------------------------------
//!	pf: tiny printf like output formatter
//!		output directly to the debug port
//!
//! @param
//!		const char 	*format:	output format
//!
//! @return
//!    Number of bytes output
//------------------------------------------------------------------------------
int pf(const char *format, ...)
{
	register int *varg = (int *)(&format);
	return print(0, varg);
}

//------------------------------------------------------------------------------
//!	spf: tiny printf like output formatter
//!		output to a supplied buffer
//!
//! @param
//!		char		*out:		output buffer
//!		const char	*format:	output format
//!
//! @return
//!    Number of bytes output
//------------------------------------------------------------------------------
int spf(char *out, const char *format, ...)
{
	register int *varg = (int *)(&format);
	return print(&out, varg);
}

//------------------------------------------------------------------------------
//!	pfv:	tiny printf like output formatter (va_list version)
//!			output directly to the debug port
//!
//! @param
//!		const char 	*format:	output format
//!		va_list	 	args:		arguments va_list (formatted by caller)
//!
//! @return
//!    Number of bytes output
//------------------------------------------------------------------------------
int pfv(const char *format, va_list args)
{
	return print_va(0, format, args);
}

//------------------------------------------------------------------------------
//!	spfv:	tiny printf like output formatter (va_list version)
//!			output to a supplied buffer
//!
//! @param
//!		char		*out:		output buffer
//!		const char	*format:	output format
//!		va_list	 	args:		arguments va_list (formatted by caller)
//!
//! @return
//!    Number of bytes output
//------------------------------------------------------------------------------
int spfv(char *out, const char *format, va_list args)
{
	return print_va(&out, format, args);
}


//
//	End: pfc.c
//
